package com.example.emos.api.controller;

import ch.qos.logback.core.util.TimeUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.*;
import com.example.emos.api.db.pojo.TbDept;
import com.example.emos.api.service.DeptService;
import io.micrometer.core.instrument.util.TimeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/dept")
@Tag(name = "DeptController", description = "部门Web接口")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/searchAllDept")
    @Operation(summary = "查询所有部门")
    public R searchAllDept(SearchAllDeptForm form) {
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        ArrayList<HashMap> list = deptService.searchAllDept(param);
        return R.ok().put("list", list);
    }
    @GetMapping("getOrgList")
    @Operation(summary = "获取组织数组")
    public R getOrgList(){
        //获取公司
        HashMap param = new HashMap();
        param.put("deptType", 1);
        ArrayList<HashMap> list = deptService.searchAllDept(param);
        for (int i = 0; i < list.size(); i++) {
          HashMap hOrg=  list.get(i);
           int orgId= (int) hOrg.get("id");
            //获取公司下面部门
            HashMap param1 = new HashMap();
            param1.put("deptSuper", orgId);
            ArrayList<HashMap> list1 = deptService.searchAllDept(param1);
            hOrg.put("childDept",list1);

        }
        return R.ok().put("list", list);
    }

    @PostMapping("/searchById")
    @Operation(summary = "根据ID查询部门")
//    @SaCheckPermission(value = {"ROOT", "DEPT:SELECT"}, mode = SaMode.OR)
    public R searchById(@Valid @RequestBody SearchDeptByIdForm form) {
        HashMap map = deptService.searchById(form.getId());
        return R.ok(map);
    }

    @PostMapping("/searchDeptByPage")
    @Operation(summary = "查询部门分页数据")
//    @SaCheckPermission(value = {"ROOT", "DEPT:SELECT"}, mode = SaMode.OR)
    public R searchDeptByPage(@Valid @RequestBody SearchDeptByPageForm form) {
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start", start);
        PageUtils pageUtils = deptService.searchDeptByPage(param);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/insert")
    @Operation(summary = "添加部门")
//    @SaCheckPermission(value = {"ROOT", "DEPT:INSERT"}, mode = SaMode.OR)
    public R insert(@Valid @RequestBody InsertDeptForm form) {
        TbDept dept = JSONUtil.parse(form).toBean(TbDept.class);
        int type=form.getDeptType();
        StringBuilder sb=  new StringBuilder();
        sb.append("AT");
        sb.append("_");
        if(type==1){
            sb.append("1");
        }else{
            sb.append("2");
        }

                sb.append("_");
       Date d= new Date();
        sb.append(d.getTime());
        dept.setDeptCode(sb.toString());
//       HashMap map= deptService.searchById(form.getDeptSuper());
//      String superName= map.get("deptName").toString();
//      dept.setDeptSuper(superName);
        int rows = deptService.insert(dept);
        if(rows>0){

            return R.ok().put("rows", rows);
        }else{
            return R.error("名称不可重复");
        }
    }

    @PostMapping("/update")
    @Operation(summary = "更新部门")
//    @SaCheckPermission(value = {"ROOT", "DEPT:UPDATE"}, mode = SaMode.OR)
    public R update(@Valid @RequestBody UpdateDeptForm form) {
        TbDept dept = new TbDept();
        dept.setId(form.getId());
        dept.setDeptName(form.getDeptName());
        dept.setDeptCode(form.getDeptCode());
        dept.setDeptType(form.getDeptType());
        dept.setDeptSuper(form.getDeptSuper());
        int rows = deptService.update(dept);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/deleteDeptByIds")
    @Operation(summary = "删除部门记录")
//    @SaCheckPermission(value = {"ROOT", "DEPT:DELETE"}, mode = SaMode.OR)
    public R deleteDeptByIds(@Valid @RequestBody DeleteDeptByIdsForm form) {
        int rows = deptService.deleteDeptByIds(form.getIds());
        return R.ok().put("rows", rows);
    }
}